package com.gupaoedu.pattern.singleton.threadlocal;

/**
 * 2019/5/11
 * created by chenpp
 * ThreadLocal 注册式单例的一种，保证在一个线程内的单例"伪线程安全"
 *
 */
public class ThreadLocalSingleton {
    private ThreadLocalSingleton(){}
    private static ThreadLocal<ThreadLocalSingleton> threadLocal = new ThreadLocal<ThreadLocalSingleton>(){
        @Override
        protected ThreadLocalSingleton initialValue() {
            System.out.println(Thread.currentThread().getName()+"执行threadlocal的初始化方法");
            return new ThreadLocalSingleton();
        }

    };
    public static  ThreadLocalSingleton getInstance(){
        return threadLocal.get();
    }
}
